package com.app.data;

import java.io.File;
import java.util.ArrayList;

public class Folder {

	private String folderPath;
	private String folderName;
	private ArrayList<String> fileList;
	private ArrayList<String> formatFileList;
	private String packangeName;
	private Project project;

	public Folder(String folderPath) {
		this.folderPath = folderPath;
		this.fileList = new ArrayList<String>();
	}
	
	public Folder(Project pro, String folderPath) {
		this.project = pro;
		this.folderPath = folderPath;
	}

	public String getFolderPath() {
		return folderPath;
	}

	public void setFolderPath(String folderPath) {
		this.folderPath = folderPath;
	}

	public ArrayList<String> getFileList() {
		return fileList;
	}

	public void setFileList(ArrayList<String> fileList) {
		this.fileList = fileList;
	}

	public void addFile(String fileName) {
		this.fileList.add(fileName);
	}

	public boolean isEmpty() {
		if (this.fileList.size() == 0)
			return true;
		else
			return false;
	}
	
	public void changeToPackageName(String projectSrcPath) {
		String target = projectSrcPath.replaceAll("\\\\", "/");
		String replacement = folderPath.replaceAll("\\\\", "/");
		String temp = replacement.replace(target, "");
		this.packangeName = temp.replaceAll("/", ".");
		
		formatFileList = new ArrayList<String>();
		for (String name : fileList) {
			String formatName = packangeName + "." + name;
			if (formatName.startsWith("."))
				formatName = formatName.replaceFirst(".", "");
			formatFileList.add(formatName);
		}
	}
	
	public ArrayList<String> getFormatFileList() {
		return formatFileList;
	} 
	
	public String getPackageName() {
		return packangeName;
	}
	
	public ArrayList<Object> getChildNodes() {
		ArrayList<Object> nodes = new ArrayList<>();
		File file = new File(project.getSrcPath());
		File[] files = file.listFiles();
		
		for (File f : files) {
			if (f.isFile()) {
				JavaFile java = new JavaFile(project, f.getAbsolutePath());
				nodes.add(java);
			} else {
				Folder folder = new Folder(project ,f.getAbsolutePath());
				nodes.add(folder);
			}
		}
		
		return nodes;
	}
	
	public String getFolderName() {
		String[] s = folderPath.replaceAll("\\\\", "/").split("/");
		folderName = s[s.length - 1];
		
		return folderName;
	}
	
	public Project getProject() {
		return project;
	}
	
	public String toString() {
		return getFolderName();
	}
}
